package com.buba.springcloud.controller;

import com.buba.springcloud.entities.CommonResult;
import com.buba.springcloud.entities.HfFoods;
import com.buba.springcloud.service.HFService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "Global_FallbackMethod")
@RequestMapping("/test")
public class HFTestController {
    @Resource
    private HFService hfService;

    @GetMapping("/findAll")
    public CommonResult findAll() {
        List<HfFoods> foods = hfService.findAll();
        return new CommonResult<>(200, "查询成功",foods);
    }

    @GetMapping("/get")
    @HystrixCommand(fallbackMethod = "FallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public CommonResult get() {
        return new CommonResult<>(200, "查询成功");
    }


    //下面是全局fallback方法
    public CommonResult Global_FallbackMethod(){
        return new CommonResult(500,"服务器异常,请稍后重试/(ㄒoㄒ)/~~");
    }

    //下面是全局fallback方法
    public CommonResult FallbackMethod(){
        return new CommonResult(666,"你真牛逼服务器搞炸了,请稍后重试/(ㄒoㄒ)/~~");
    }
}
